package rhinestone.Control;

import rhinestone.Model.Customer;
import rhinestone.Model.CustomerContainer;

/**
 * Handles all the stuff with customers.
 *
 * @author Group 2
 * @version 2010.12.17
 */
public class CustomerController {
	private CustomerContainer customerContainer;

	public CustomerController(CustomerContainer customerContainer) {
		this.customerContainer = customerContainer;
	}

	public void addCustomer(String name, String address, String cpr, String phone, double generalDiscount, double craftsmanDiscount, double deliveryDiscount) {
		Customer customer = new Customer(name, address, cpr, phone, generalDiscount, craftsmanDiscount, deliveryDiscount);
		customerContainer.addCustomer(customer);
		System.out.println(" --- Customer added ---");
	}

	public Customer getCustomer(String cpr) {
		return customerContainer.getCustomer(cpr);
	}

	public void updateCustomer(Customer customer, String name, String address, String cpr, String phone, String generalDiscount, String craftsmanDiscount, String deliveryDiscount) {
		if(!name.equals(""))
			customer.setName(name);
		if(!address.equals(""))
			customer.setAddress(address);
		if(!cpr.equals(""))
			customer.setCpr(cpr);
		if(!phone.equals(""))
			customer.setPhone(phone);
		if(!generalDiscount.equals("")) {
			try {
				customer.setGeneralDiscount(Double.parseDouble(generalDiscount));
			} catch(NumberFormatException ex) {
				System.err.println("General discount not changed, wrong number format: " + generalDiscount);
			}
		}
		if(!craftsmanDiscount.equals("")) {
			try {
				customer.setCraftsmanDiscount(Double.parseDouble(craftsmanDiscount));
			} catch(NumberFormatException ex) {
				System.err.println("Craftsman discount not changed, wrong number format: " + craftsmanDiscount);
			}
		}
		if(!deliveryDiscount.equals("")) {
			try {
				customer.setDeliveryDiscount(Double.parseDouble(deliveryDiscount));
			} catch(NumberFormatException ex) {
				System.err.println("Delivery discount not changed, wrong number format: " + deliveryDiscount);
			}
		}

		customerContainer.addCustomer(customer);
		System.out.println("Customer updated");
	}

	public Customer deleteCustomer(String cpr) {
		return customerContainer.deleteCustomer(cpr);
	}
}
